package io.osw.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.osw.common.utils.FileUtil;
import io.osw.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.osw.modules.sys.entity.TblMtlItemEntity;
import io.osw.modules.sys.service.TblMtlItemService;
import io.osw.common.utils.PageUtils;
import io.osw.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author guxiaoguang
 * @email xuebai864627555@gmail.com
 * @date 2019-01-03 21:52:49
 */
@RestController
@RequestMapping("sys/tblmtlitem")
public class TblMtlItemController extends AbstractController{
    @Autowired
    private TblMtlItemService tblMtlItemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:tblmtlitem:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tblMtlItemService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{itemId}")
    @RequiresPermissions("sys:tblmtlitem:info")
    public R info(@PathVariable("itemId") Long itemId){
        TblMtlItemEntity tblMtlItem = tblMtlItemService.selectById(itemId);

        return R.ok().put("tblMtlItem", tblMtlItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:tblmtlitem:save")
    public R save(@RequestBody TblMtlItemEntity tblMtlItem){
        tblMtlItem.setCreateTime(new Date());
        tblMtlItem.setCreateBy(getUserId());
        tblMtlItem.setCompanyId(getUser().getCompanyId());
        tblMtlItemService.insert(tblMtlItem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:tblmtlitem:update")
    public R update(@RequestBody TblMtlItemEntity tblMtlItem){
        ValidatorUtils.validateEntity(tblMtlItem);
        tblMtlItem.setUpdateTime(new Date());
        tblMtlItem.setUpdateBy(getUserId());
        tblMtlItemService.updateAllColumnById(tblMtlItem);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:tblmtlitem:delete")
    public R delete(@RequestBody Long[] itemIds){
        tblMtlItemService.deleteBatchIds(Arrays.asList(itemIds));

        return R.ok();
    }

    /**
     * 图片上传
     * @param img
     * @return
     */
    @RequestMapping(value="/uploadImg",method = RequestMethod.POST)
    @ResponseBody
    public String uploadImg(@RequestParam("image") MultipartFile img,String name){
        //图片上传调用工具类
        try{
            //保存图片
            String path =  FileUtil.saveImg(img,name);
            return path;
        }catch (Exception e){
            return "上传图片失败";
        }
    }
}
